package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.base.Page;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.SpuManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SpuManageServiceImpl implements SpuManageService {
    @Autowired
    private SpuManageMapper spuManageMapper;
    @Autowired
    private SpuImageMapper spuImageMapper;
    @Autowired
    private SpuPosterMapper spuPosterMapper;
    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;
    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;


    @Override
    public Page<SpuInfo> getSpuByPage(Long pageNum, Long pageSize, Long category3Id) {
        int offset = (pageNum.intValue() - 1) * pageSize.intValue();
        int limit = pageSize.intValue();
        List<SpuInfo> spuInfoList = spuManageMapper.getSpuByPage(offset, limit, category3Id);
        int total = spuManageMapper.countAll();

        Page<SpuInfo> page = new Page<>();
        page.setRecords(spuInfoList);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page.setTotal((long) total);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {
        if(null==spuInfo){
            throw new NullPointerException("spuInfo的数据为空");
        }
        spuManageMapper.saveSpuInfo(spuInfo);
        Long spuId = spuInfo.getId();
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if(CollectionUtils.isEmpty(spuImageList)){
            throw new NullPointerException("spuImage的数据为空");
        }
        spuImageMapper.saveSpuImageList(spuId,spuImageList);
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if(CollectionUtils.isEmpty(spuPosterList)){
            throw new NullPointerException("spuPoster的数据为空");
        }
        spuPosterMapper.saveSpuPosterList(spuId,spuPosterList);
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if(CollectionUtils.isEmpty(spuSaleAttrList)){
            throw new NullPointerException("spuSaleAttr的数据为空");
        }
        for (SpuSaleAttr spuSaleAttr : spuSaleAttrList) {
            List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
            if(CollectionUtils.isEmpty(spuSaleAttrValueList)){
                throw new NullPointerException("spuSaleAttrValue的数据为空");
            }
            String saleAttrName = spuSaleAttr.getSaleAttrName();
            spuSaleAttrValueMapper.saveSpuSaleAttrValueList(spuId,saleAttrName,spuSaleAttrValueList);
        }
        spuSaleAttrMapper.saveSpuSaleAttrList(spuId,spuSaleAttrList);
    }

    @Override
    public List<SpuImage> getSpuImageList(Long spuId) {
        QueryWrapper<SpuImage> spuImageQueryWrapper = new QueryWrapper<>();
        spuImageQueryWrapper.eq("spu_id",spuId );
       return spuImageMapper.selectList(spuImageQueryWrapper);
    }

    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {
      List<SpuSaleAttr>  spuSaleAttrList =spuSaleAttrMapper.getSpuSaleAttrList(spuId);
        return spuSaleAttrList;
    }

    @Override
    public List<SpuPoster> findSpuPosterBySpuId(Long spuId) {
     return    spuPosterMapper.findSpuPosterBySpuId(spuId);
    }

    @Override
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(Long skuId, Long spuId) {

        return spuSaleAttrMapper.getSpuSaleAttrListCheckBySku(skuId,spuId);
    }


}
